package routes

import (
	"devops/config"
	"devops/controller"
	_ "devops/docs"
	"devops/middleware"

	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func InitRoutes() *gin.Engine {
	router := gin.Default()

	router.NoRoute(controller.NoRouter)

	router.Any("/", controller.Hello)
	// 应用接口增加采样
	router.Use(middleware.Prometheus())
	router.GET("/metrics", gin.WrapH(promhttp.Handler()))

	api := router.Group("/api")

	// swagger

	url := ginSwagger.URL(config.SwaggerUrl + ":" + config.SrvPort + "/api/swagger/doc.json")
	api.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

	// 第三方仓库信息
	git := api.Group("/git")
	{
		git.GET("/user/repos", controller.GetRepos)
		git.GET("/user/repo/branches", controller.GetBranches)
		git.POST("/webhook", controller.WeebHook)
	}

	api.Use(middleware.TsfAuth())
	// 项目
	project := api.Group("/project")
	{
		project.GET("/all", controller.GetAllProject)
		project.GET("/dashboard", controller.GetProjectDashboard)
		project.GET("/build/dashboard", controller.GetBuildDashboard)

		project.POST("/create", controller.CreateProject)
		project.POST("/delete", controller.DeleteProject)
	}

	// 构建计划
	ci := api.Group("/ci")
	{
		ci.GET("/all", controller.GetProjectCI)
		ci.GET("/one", controller.GetOneCI)
		ci.GET("/one/baseinfo", controller.GetOneCIBaseInfo)

		ci.POST("/create", controller.CreateCI)
		ci.POST("/update", controller.UpdateCI)
		ci.POST("/name/update", controller.UpdateCiName)
		ci.POST("/updateJksfile", controller.UpdateJksfile)
		ci.POST("/delete", controller.DeleteCI)

		env := ci.Group("/env")
		{
			env.GET("/all", controller.GetCiEnvs)
			env.GET("/one", controller.GetCiEnv)
			env.POST("/create", controller.CreateCIEnv)
			env.POST("/update", controller.UpdateCiEnv)
			env.POST("/delete", controller.DeleteCIEnv)
		}

		setting := ci.Group("/setting")
		{
			setting.GET("/discarder", controller.GetDiscarder)
			setting.POST("/discarder/set", controller.SetDiscarder)

			setting.GET("/timing", controller.GetTiming)
			setting.POST("/timing/set", controller.SetTiming)
		}
		ci.POST("/jenkins/tojson", controller.ToJson)
		ci.POST("/jenkins/tojenkinsfile", controller.ToJenkinsfile)
		ci.POST("/jenkins/validateJson", controller.ValidateJson)
	}

	// 制品库
	docker := api.Group("/docker")
	{
		docker.GET("/repos", controller.GetDockerRepos)
	}

	// 构建记录
	pipeline := api.Group("/pipeline")
	{
		pipeline.POST("/run", controller.RunPipline)
		pipeline.POST("/stop", controller.StopPipline)
		pipeline.POST("/again", controller.AgainPipline)

		//pipeline.GET("/build/one", controller.GetPipline)
		pipeline.GET("/build/all", controller.GetAllPipline)
		pipeline.GET("/build/stage", controller.GetStage)
		pipeline.POST("/build/stage/simple", controller.GetStageSimple)
		pipeline.GET("/build/stage/flow/log", controller.GetStageInfoLog)
		pipeline.GET("/build/testReport", controller.GetTestReport)
		pipeline.GET("/build/artifacts", controller.GetArtifacts)
		pipeline.GET("/build/artifacts/download", controller.DownloadArtifact)
		pipeline.GET("/build/console/output", controller.GetBuildConsoleOutput)

		// 人工确认
		pipeline.GET("/build/input/action", controller.GetInputAction)
		pipeline.POST("/build/input/submit", controller.InputSubmit)

		pipeline.POST("/build/delete", controller.DeleteBuild)
	}

	event := api.Group("/event")
	{
		event.GET("/all", controller.GetAllEvent)
	}

	// jenkinsfile 模版
	tmp := api.Group("tmp")
	{
		tmp.GET("/all", controller.GetTemplate)
	}

	cd := api.Group("cd")
	{
		cd.POST("/create", controller.CreateCD)
		cd.GET("/one", controller.GetCD)
		cd.GET("/deploy/groups", controller.GetDeployGroups)
		cd.GET("/deploy/apps", controller.GetApps)
	}
	return router
}
